<template>
  <a-card :bordered="false">
    <div>
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <div style="display: flex; padding: 14px 0 7px 0;align-items: center">
          <div style="width: 140px; line-height: 32px; font-weight: bold; color: #000">{{ $t('礼品定价规则') }}</div>
          <a-row :gutter="[0, 24]" style="width: calc(100% - 140px)">
            <a-col :span="24">
              <a-checkbox :checked="value" @change="change">{{ $t('启用') }}</a-checkbox>
              <span style="margin-left: 20px; color: orange">{{
                $t('说明：当添加/修改商品资料时，若商品资料尚未设置兑换价值则系统会根据兑换规则自动计算默认的兑换值，根据需要可人工再调整')
              }}</span>
            </a-col>
          </a-row>
        </div>
        <a-row>
          <a-col :span="23" :offset="2" style="display: flex; align-items: center; padding: 7px 0">
            <span style="color:rgba(0,0,0,0.85)">{{ $t('代币兑换值') }}：</span>
            <a-input-number v-model="coinRule.dvalue" :disabled="!value" style="width: 120px"

                            :formatter="formatter"
                                  :parser="parser"

            ></a-input-number>
            <span> × {{ $t('售价') }}</span>
          </a-col>
          <a-col :span="23" :offset="2" style="display: flex; align-items: center; padding: 7px 0">
            <span style="color:rgba(0,0,0,0.85)">{{ $t('彩票兑换值') }}：</span>
            <a-input-number v-model="lotteryRule.dvalue" :disabled="!value" style="width: 120px"

                            :formatter="formatter"
                                  :parser="parser"

                            :precision="2"
            ></a-input-number>
            <span> × {{ $t('售价') }}</span>
          </a-col>
          <a-col :span="23" :offset="2" style="display: flex; align-items: center; padding: 7px 0">
            <span style="color:rgba(0,0,0,0.85)">{{ $t('积分兑换值') }}：</span>
            <a-input-number v-model="integralRule.dvalue" :disabled="!value" style="width: 120px"

                            :formatter="formatter"
                                  :parser="parser"

            ></a-input-number>
            <span> × {{ $t('售价') }}</span>
          </a-col>
          <a-col :span="23" :offset="2" style="display: flex; align-items: center; padding: 7px 0">
            <span style="color:rgba(0,0,0,0.85)">{{ $t('A币兑换值') }}：</span>
            <a-input-number v-model="pointsRule.dvalue" :disabled="!value" style="width: 120px"

                            :formatter="formatter"
                                  :parser="parser"

            ></a-input-number>
            <span> × {{ $t('售价') }}</span>
          </a-col>
          <a-col :span="23" :offset="2" style="display: flex; align-items: center; padding: 7px 0">
            <span style="color:rgba(0,0,0,0.85)">{{ $t('A票兑换值') }}：</span>
            <a-input-number v-model="alotteryRule.dvalue" :disabled="!value" style="width: 120px"

                            :formatter="formatter"
                                  :parser="parser"

            ></a-input-number>
            <span> × {{ $t('售价') }}</span>
          </a-col>
          <a-col :span="23" :offset="2" style="display: flex; padding: 7px 0">
            <a-button type="primary" @click="save" :disabled="disabled" v-has="'BisSysParameterController:edit'">{{ $t('保存') }}</a-button>
          </a-col>
        </a-row>
        <div style="display: flex; padding: 14px 0">
          <div style="width: 140px; line-height: 32px; font-weight: bold; color: #000">{{ $t('金额显示小数') }}</div>
          <a-row :gutter="[0, 24]" style="width: calc(100% - 140px)">
            <a-col :span="24">
              <a-checkbox v-model="isPriceDecimal.ivalues">{{ $t('启用') }}</a-checkbox>
              <a-button type="primary" style="margin-left: 20px" @click="parameterChange(isPriceDecimal)" v-has="'BisSysParameterController:edit'">{{
                $t('保存')
              }}</a-button>
            </a-col>
          </a-row>
        </div>
        <div style="display: flex; padding: 14px 0">
          <div style="width: 140px; line-height: 32px; font-weight: bold; color: #000">{{ $t('新商品入库') }}</div>
          <a-row :gutter="[0, 24]" style="width: calc(100% - 140px)">
            <a-col :span="24">
              <a-checkbox v-model="newStockIn.ivalues">{{ $t('启用') }}</a-checkbox>
              <a-button type="primary" style="margin-left: 20px" @click="parameterChange(newStockIn)" v-has="'BisSysParameterController:edit'">{{
                $t('保存')
              }}</a-button>
              <span style="margin-left: 20px; color: orange">{{
                $t('（开启后支持新增商品时直接选择仓库设定库存数量初始化仓库库存）')
              }}</span>
            </a-col>
          </a-row>
        </div>
      </a-form>
    </div>
  </a-card>
</template>
<script>
import { getAction, postAction, httpAction } from '@/api/manage'
import {formatter, parser} from "@api/comm";
export default {
  data() {
    return {
      formatter,
      parser,
      value: false,
      isGoodsPriceRule: {
        name: 'isGoodsPriceRule',
        ivalue: 0,
        comment: this.$t('是否启用礼品定价规则'),
      },
      coinRule: {
        name: 'coinRule',
        dvalue: '1',
        comment: this.$t('代币兑换值'),
      },
      lotteryRule: {
        name: 'lotteryRule',
        dvalue: '1',
        comment: this.$t('彩票兑换值'),
      },
      integralRule: {
        name: 'integralRule',
        dvalue: '1',
        comment: this.$t('积分兑换值'),
      },
      pointsRule: {
        name: 'pointsRule',
        dvalue: '1',
        comment: this.$t('A币兑换值'),
      },
      alotteryRule: {
        name: 'alotteryRule',
        dvalue: '1',
        comment: this.$t('A票兑换值'),
      },
      isPriceDecimal: {
        name: 'isPriceDecimal',
        ivalues: true,
        ivalue: 1,
        comment: this.$t('金额保留小数'),
      },
      newStockIn: {
        name: 'newStockIn',
        ivalues: false,
        ivalue: 0,
        comment: this.$t('新商品入库'),
      },
      disabled: false,
      labelCol: {
        xs: { span: 24 },
        md: { span: 10 },
      },
      wrapperCol: {
        xs: { span: 24 },
        md: { span: 20 },
      },
      disabled1: false,
      value1: true,
    }
  },
  mounted() {
    let that = this
    that.getInfo()
  },
  methods: {
    getInfo() {
      let that = this
      that.getParameter(that.isPriceDecimal)
      that.getParameter(that.newStockIn)
      getAction('/bis/bisSysParameter/list', { name: 'isGoodsPriceRule' }).then((res) => {
        if (res.success) {
          if (res.result.total == 0) {
            that.value = false
          } else {
            if (res.result.records[0].ivalue) {
              that.value = true
            } else {
              that.value = false
            }
            that.isGoodsPriceRule = res.result.records[0]
          }
        } else {
          that.$message.error(res.message)
        }
      })
      getAction('/bis/bisSysParameter/list', { name: 'coinRule' }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.coinRule = res.result.records[0]
          }
        } else {
          that.$message.error(res.message)
        }
      })
      getAction('/bis/bisSysParameter/list', { name: 'lotteryRule' }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.lotteryRule = res.result.records[0]
          }
        } else {
          that.$message.error(res.message)
        }
      })
      getAction('/bis/bisSysParameter/list', { name: 'integralRule' }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.integralRule = res.result.records[0]
          }
        } else {
          that.$message.error(res.message)
        }
      })
      getAction('/bis/bisSysParameter/list', { name: 'pointsRule' }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.pointsRule = res.result.records[0]
          }
        } else {
          that.$message.error(res.message)
        }
      })
      getAction('/bis/bisSysParameter/list', { name: 'alotteryRule' }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.alotteryRule = res.result.records[0]
          }
        } else {
          that.$message.error(res.message)
        }
      })
    },
    change(e) {
      this.value = e.target.checked
      if (e.target.checked) {
        this.isGoodsPriceRule.ivalue = 1
      } else {
        this.isGoodsPriceRule.ivalue = 0
      }
    },
    save() {
      let that = this,
        url = ''
      that.disabled = true
      if (that.isGoodsPriceRule.id) {
        url = '/bis/bisSysParameter/edit'
      } else {
        url = '/bis/bisSysParameter/add'
      }
      postAction(url, that.isGoodsPriceRule).then((res) => {
        if (res.success) {
          postAction(url, that.coinRule).then((res) => {
            if (res.success) {
              postAction(url, that.lotteryRule).then((res) => {
                if (res.success) {
                  postAction(url, that.integralRule).then((res) => {
                    if (res.success) {
                      postAction(url, that.pointsRule).then((res) => {
                        if (res.success) {
                          postAction(url, that.alotteryRule).then((res) => {
                            if (res.success) {
                              that.$message.success(this.$t('保存成功'))
                              that.disabled = false
                              that.getInfo()
                            } else {
                              that.$message.error(res.message)
                              that.disabled = false
                            }
                          })
                        } else {
                          that.$message.error(res.message)
                          that.disabled = false
                        }
                      })
                    } else {
                      that.$message.error(res.message)
                      that.disabled = false
                    }
                  })
                } else {
                  that.$message.error(res.message)
                  that.disabled = false
                }
              })
            } else {
              that.$message.error(res.message)
              that.disabled = false
            }
          })
        } else {
          that.$message.error(res.message)
          that.disabled = false
        }
      })
    },
    getParameter(model) {
      let that = this
      getAction('/bis/bisSysParameter/list', { name: model.name }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.$set(that, model.name, res.result.records[0])
            that.$set(that[model.name], "ivalues", that[model.name].ivalue ? true : false)
            if(model.name == 'newStockIn'){
              localStorage.setItem(model.name, that[model.name].ivalue)
            }
          }
        } else {
          that.$message.error(res.message)
        }
      })
    },
    parameterChange(model) {
      let that = this,querydata = {}
      let url = '', method = ''
      if (model.id) {
        url = '/bis/bisSysParameter/edit'
        method = 'put'
      } else {
        url = '/bis/bisSysParameter/add'
        method = 'post'
      }
      if (model.ivalues === true) {
        model.ivalue = 1
      } else {
        model.ivalue = 0
      }
      querydata = Object.assign({}, model)
      httpAction(url, querydata, method).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.getParameter(model)
        } else {
          that.$message.warning(res.message)
        }
      })
    },
  },
}
</script>
<style scoped>
</style>